<template>
	<div>
		<view class="wrap">
			<view class="wrap-banner">
				<swiper circular autoplay>
					<block v-for="(item, index) in banner" :key="index">
						<swiper-item>
							<image class="img" :src="getImageURL(item.url)" lazy-load @error="onBannerError(index)" />
						</swiper-item>
					</block>
				</swiper>
			</view>
			
			<view class="wrap-list">
				<view class="list-content">
					<block v-for="(item, index) in list" :key="index">
						<view class="item" @click="onItem(item)">
							<image class="img" :src="getImageURL(item.cover)" lazy-load @error="onImageError(index)" />
							<view class="title limit-num-line">{{item.title}}</view>
						</view>
					</block>
				</view>
			</view>
		</view>
	</div>
</template>

<script>
	import {guideListData} from "@/utils/api.js";
	export default {
		data() {
			return {
				banner: [], //轮播图数组
				
				page: 1,
				total: 0,
				list: [],
			}
		},
		onLoad() {
			this.$filters.getConfig(res => {
				this.banner = res.competitionGuide;
			})
		},
		onShow() {
			const loginStatus = this.$filters.isLogin()
			if (loginStatus === true) {
				this.onRefresh()
			}
		},
		onPullDownRefresh() {
			this.onRefresh();
		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++;
				this.getData()
			}
		},
		methods: {
			/**
			 * 图片加载失败
			 */
			onImageError(index) {
				this.list[index].cover = "/static/common/default_icon.png";
			},
			ononBannerError(index) {
				this.banner[index].url = "/static/common/default_icon.png";
			},
			/**
			 * 详情
			 */
			onItem(item) {
				if (this.$filters.isGoToExternalLink(item.isLink, item.content)) {
					uni.navigateTo({
						url: `/pages/eventGuide/detail?id=${item.competitionGuideId}`
					})
				}
			},
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1;
				this.list = []
				this.getData()
			},
			/**
			 * 网络请求
			 */
			getData() {
				guideListData(this.page).then(res => {
					this.total = res.data.total;
					const list = res.data.data;
					if (this.page === 1) {
						this.list = list;
					} else {
						this.list = this.list.concat(list);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: white;
	}
	.wrap {
		width: 100%;
		overflow: hidden;
		
		.wrap-banner {
			width: 100%;
			height: 576rpx;
			
			swiper, swiper-item {
				width: 100%;
				height: 100%;
			}
			.img {
				width: 100%;
				height: 576rpx;
			}
		}
		
		.wrap-list {
			margin-top: 47rpx;
			width: 100%;
			overflow: hidden;
			padding: 0rpx 33rpx;
			box-sizing: border-box;
			
			.list-content {
				margin-left: -58rpx;
				width: calc(100% + 58rpx);
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
			}
			
			.item {
				margin-left: 58rpx;
				width: 190rpx;
				overflow: hidden;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 40rpx;
				
				.img {
					width: 190rpx;
					height: 126rpx;
				}
				.title {
					--limit-num: 1;
					margin-top: 20rpx;
					color: #333333;
					font-size: 26rpx;
				}
			}
		}
	}
</style>